package com.clz.mycustomview.view;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.clz.mycustomview.R;

public class BaseView extends View {
    private Context mContext;
    public BaseView(Context context) {
        super(context);
        this.mContext = context;
    }

    public BaseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public BaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔的基本属性
        Paint paint = new Paint();
        // 设置画笔颜色
        paint.setColor(Color.RED);
        // 设置填充样式
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(50);
        // 画圆
        //canvas.drawCircle(190, 200, 150, paint);

        // 两个不同的圆
        paint.setColor(0XFFFF0000);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(50);
        //canvas.drawCircle(210,210,150,paint);

        paint.setColor(0x7EFFFF00);
        // canvas.drawCircle(210,210,100,paint);


        // canvas的使用
        // 设置画布的颜色
        // canvas.drawColor(Color.YELLOW);
        // canvas.drawRGB(0XFF,0x00,0xFF);
        //canvas.drawARGB(0xEF,0xFF,0,0xFF);

        // 画直线
        //canvas.drawLine(50,200,50,250,paint);
        // 画多条直线
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        // (10,10) 和(100，100) (200,200)和(400,400)
        float[] lines = {10, 10, 100, 100, 200, 200, 400, 400};
        // canvas.drawLines(lines,paint);
        /**
         * int offset 集合中跳过的数值个数
         * int count 参与绘制的数值个数
         * 下例只有(100,100) 和(200,200)两个点被绘制了
         */
        //canvas.drawLines(lines,2,4,paint);
        // 画点
        //canvas.drawPoint(120, 120, paint);
        //画矩形
        Rect rect = new Rect();
        rect.set(10, 10, 100, 100);
        //canvas.drawRect(rect, paint);
        // 画圆角矩形
        /**
         * float x 生成圆角的椭圆的X轴半径
         * float y 生成圆角的椭圆的y轴半径
         */
        RectF rectF = new RectF(120, 10, 300, 190);
        //canvas.drawRoundRect(rectF, 30, 30, paint);

        // 画圆形
        paint.setStrokeWidth(30);
        paint.setColor(Color.BLUE);
        //canvas.drawCircle(100,80,60,paint);
        // 画椭圆
        RectF rectF1 = new RectF(50, 80, 400, 400);
        //canvas.drawOval(rectF1,paint);
        // 画弧
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);
        /**
         * boolean useCenter 是否有弧的2边
         */
        RectF rectF2 = new RectF(50, 80, 400, 180);
        //canvas.drawArc(rectF2,0,90,true,paint);
        // 画路径
        // 直线路径(三角形)
        Path path = new Path();
        path.moveTo(10, 10);
        path.lineTo(10, 100);
        path.lineTo(300, 100);
        path.close();
        //canvas.drawPath(path,paint);
        // 弧线路径
        //(连贯的弧线路径)
        Path path1 = new Path();
        path1.moveTo(10, 10);
        RectF rectF3 = new RectF(100, 10, 200, 100);
        path1.arcTo(rectF3, 0, 90);
        paint.setStrokeWidth(3);
        //canvas.drawPath(path1,paint);
        // 不连贯的弧线路径
        Path path2 = new Path();
        RectF rectF4 = new RectF(100, 100, 150, 150);
        path2.moveTo(10, 10);
        path2.lineTo(100, 50);
        path2.addArc(rectF4, 0, 90);
        //canvas.drawPath(path2,paint);

        // 根据路径方向布局文字
        // 先创建两个大小相同的路径
        // 第一条逆向路径生成
        Path CCWRectpath = new Path();
        RectF CCWRect = new RectF(50, 50, 240, 200);
        CCWRectpath.addRect(CCWRect, Path.Direction.CCW);
        // canvas.drawPath(CCWRectpath,paint);
        // 第二条顺向路径生成
        Path CWRectpath = new Path();
        RectF CWRect = new RectF(290, 50, 480, 200);
        CWRectpath.addRect(CWRect, Path.Direction.CW);
        //canvas.drawPath(CWRectpath,paint);

        // 根据路径布局文字
        String text = "苦心人天不负，有志者事竟成";
        paint.setColor(Color.GREEN);
        paint.setTextSize(35);
        // canvas.drawTextOnPath(text,CCWRectpath,0,18,paint); //逆时针方向生成
        //canvas.drawTextOnPath(text,CWRectpath,0,18,paint);  // 顺时针方向生成

        // 文字
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);
        // 指定是否使用抗锯齿功能，如果使用，则会使速度变慢
        paint.setAntiAlias(true);
        // paint.setStyle(Paint.Style.FILL);
        // paint.setStyle(Paint.Style.FILL_AND_STROKE);
        //paint.setStyle(Paint.Style.STROKE);
        //设置粗体文字
        //paint.setFakeBoldText(true);
        // 设置下划线
        // paint.setUnderlineText(true);
        // 设置带有删除线效果
        // paint.setStrikeThruText(true);

        // 设置字体倾斜度 float skewX 取负值，文字向右倾斜，取正值时，文字向左倾斜
        //正常样式
        paint.setTextSize(80);
        //canvas.drawText("床前明月光", 10, 100, paint);
        // 向右倾斜
        //paint.setTextSkewX(-0.25f);
        // canvas.drawText("床前明月光",10,200,paint);
        // 向左倾斜
        //paint.setTextSkewX(0.25f);
        // canvas.drawText("床前明月光",10,300,paint);

        // 水平拉伸
        //paint.setTextScaleX(2);
        // canvas.drawText("床前明月光",10,200,paint);
        /**
         * 逐个指定文字位置
         * char[] text 要绘制的字符串
         * int index 第一个要绘制的文字的索引
         * int count 要绘制的文字的个数
         * float[] pos 要绘制的文字的个数
         */
        float post[] = new float[]{80, 100, 80, 200, 80, 300, 80, 400};
        //canvas.drawPosText("床前明月", post, paint);

        // 设置自定义字体
        paint.setTextSize(60);
        AssetManager mgr = mContext.getAssets();
        //根据路径 得到Typeface
        Typeface typeface = Typeface.createFromAsset(mgr, "fonts/jian_luobo.ttf");
        paint.setTypeface(typeface);
        canvas.drawText("床前明月光，疑是地上霜",10,100,paint);
    }
}
